<?php

namespace App\Helpers;

use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

trait ApiResponseTrait
{
    /**
     * 返回分页数据的自定义响应.
     *
     * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $data
     * @param int $status
     * @param array $headers
     * @param int $options
     * @return JsonResponse
     */
    public function paginate(\Illuminate\Contracts\Pagination\LengthAwarePaginator $data, int $status = ResponseAlias::HTTP_OK, array $headers = [], int $options = 0): JsonResponse
    {
        $response = [
            'code' => $status,
            'message' => '操作成功',
            'data' => [
                'items' => $data->items(),
                'meta' => [
                    'total' => $data->total(),
                    'per_page' => $data->perPage(),
                    'current_page' => $data->currentPage(),
                    'last_page' => $data->lastPage(),
                    'from' => $data->firstItem(),
                    'to' => $data->lastItem()
                ]
            ],
        ];

        return new JsonResponse($response, $status, $headers, $options);
    }

    /**
     * 成功响应.
     *
     * @param mixed $data
     * @param int $status
     * @param array $headers
     * @param int $options
     * @param array $extraData
     * @return JsonResponse
     */
    public function success($data = [], int $status = ResponseAlias::HTTP_OK, array $headers = [], int $options = 0, array $extraData = []): JsonResponse
    {
        $response = [
            'code' => $status,
            'message' => '操作成功',
            'data' => $data,
        ];

        // 合并额外的数据
        $response = array_merge($response, $extraData);

        return new JsonResponse($response, $status, $headers, $options);
    }

    /**
     * 错误响应.
     *
     * @param string $message
     * @param int $status
     * @param array $headers
     * @param int $options
     * @param array $extraData
     * @return JsonResponse
     */
    public function error(string $message = '发生错误', int $status = ResponseAlias::HTTP_BAD_REQUEST, array $headers = [], int $options = 0, array $extraData = []): JsonResponse
    {
        $response = [
            'code' => $status,
            'message' => $message,
        ];

        // 合并额外的数据
        $response = array_merge($response, $extraData);

        return new JsonResponse($response, $status, $headers, $options);
    }

    /**
     * 未找到资源的响应.
     *
     * @param string $message
     * @param array $headers
     * @param int $options
     * @param array $extraData
     * @return JsonResponse
     */
    public function notFound(string $message = '资源未找到', array $headers = [], int $options = 0, array $extraData = []): JsonResponse
    {
        return $this->error($message, ResponseAlias::HTTP_NOT_FOUND, $headers, $options, $extraData);
    }

    /**
     * 未经授权的响应.
     *
     * @param string $message
     * @param array $headers
     * @param int $options
     * @param array $extraData
     * @return JsonResponse
     */
    public function unauthorized(string $message = '未授权', array $headers = [], int $options = 0, array $extraData = []): JsonResponse
    {
        return $this->error($message, ResponseAlias::HTTP_UNAUTHORIZED, $headers, $options, $extraData);
    }

    /**
     * 未经过验证的响应.
     *
     * @param string $message
     * @param array $headers
     * @param int $options
     * @param array $extraData
     * @return JsonResponse
     */
    public function unprocessableEntity(string $message = '验证失败', array $headers = [], int $options = 0, array $extraData = []): JsonResponse
    {
        return $this->error($message, ResponseAlias::HTTP_UNPROCESSABLE_ENTITY, $headers, $options, $extraData);
    }
}
